<?php
/**
 * Enhance開発 告知メール
 * @author nankuifeng
 *
 */
include("components/parameter_util.php");
class NoticeMailController extends AppController
{				
	var $name = 'NoticeMail';
	var $uses = array (
		'Member'
		,'NoticeMsgInfo'	
		,'NoticeMsgReactionInfo'
		,'SpecialPublicInfo'
		,'Company'
		,'CompanyAccount'
		,'MemberEmailMsg'
	);
	
	function index(){
		$this->render('index');
	}

	/**
	 * 告知メールの詳細内容を表示する
	 */
	function  noticeMailDetail($notice_msg_id,$status) {
		$member_id = $this->rdAuth->id;
		if (empty($member_id)) {
			$this->redirect(FRONT_SERVER_HTTP.FRONT_SERVER_NAME.'/memberLogin/login/member/remmember');
		}
		//设置后退失效
		$this->setHead();
	
		//パラメータのチェック
		$notice_msg_id = getSafeParameter($notice_msg_id);
		$status = getSafeParameter($status);
		
		if(empty($notice_msg_id) || !is_numeric($notice_msg_id)
				|| $status == null || !is_numeric($status)) {
			$this->show404();
			exit;
		}
		
		$this->pageTitle='メッセージ詳細';

		if ($status == '0') {
			$this->NoticeMsgReactionInfo->updateReadedStatus($notice_msg_id);
		}
		$msgDetail = $this->NoticeMsgInfo->getNoticeMsgInfo($notice_msg_id);
		if($msgDetail == null) {
			$this->show404();
			exit;			
		}
			
		$msgDetail['notice_msg_id'] = $notice_msg_id;
		$msgDetail['received'] = date("Y/m/d H:i",strtotime($msgDetail['received']));
		$msgDetail['contents'] = str_ireplace("\r\n", "<br>", $msgDetail['contents']);
		$msgDetail['contents'] = str_ireplace("\n", "<br>", $msgDetail['contents']);
		
		$member_data = $this->Member->find('Member.id = ' . $member_id . ' and Member.deleted is null ');
		$msgDetail['contents'] = str_ireplace("%id%", $member_data['Member']['id'], $msgDetail['contents']);
		
		//print_r($member_data);exit;
		if($member_data['Member']['public_profile_flg'] == '1') {
			$special_status = '0';
			
			$spe_data = $this->SpecialPublicInfo->find("SpecialPublicInfo.member_id = " . $member_id 
					." and SpecialPublicInfo.company_id =  " .$msgDetail['company_id'] . "");

			if($spe_data == null || empty($spe_data)) {
				//特別公開していない
				$special_status = '0';
			} else if(!empty($spe_data['SpecialPublicInfo']['deleted'])) {
				//特別公開データを削除している
				$special_status = '1';
			} else if($spe_data['SpecialPublicInfo']['public_flg'] == '1') {
				//特別公開している
				$special_status = '2';
			}
			$msgDetail['special_status'] = $special_status;
		} elseif($member_data['Member']['public_profile_flg'] == '2') {
			//公開している
			$msgDetail['special_status'] = '2';
		}
		$this->set("msgDetail", $msgDetail);
		$this->render('noticeMailDetail');		
		
	}
	
	/**
	 * 告知メールの企業に特別公開する
	 */
	function specialPublic($company_id, $notice_msg_id) {
		$member_id = $this->rdAuth->id;
		if (empty($member_id)) {
			$this->redirect(FRONT_SERVER_HTTP.FRONT_SERVER_NAME.'/memberLogin/login/member/remmember');
		}

		if(empty($company_id) || !is_numeric($company_id)) {
			$this->show404();
			exit;
		}
		// ADD tsubasa 2012/01/31 start
		$this->Member->save(array("id" => $member_id));
		// ADD tsubasa 2012/01/31 end
		$special[$company_id]['public_flg']= '1';
		$this->SpecialPublicInfo->doSaveSpecialCompany('1', $special, $member_id);
		
		$this->noticeMailDetail($notice_msg_id, '1');
	}
		
	
	/**
	 * 后退页面处理
	 * */
	private function setHead() {
		header("Expires: Mon, 26 Jul 1970 05:00:00 GMT");
		header("Last-Modified:".gmdate("D, d M Y H:i:s") . " GMT");
		header("Cache-Control: no-cache, must-revalidate");
		header("Pragma: no-cache");
	}	
	
}

?>